
import facepp
import json
import time

########################################################################################################################

def annotate_photos(api, base_dir):
	photos_urls_file = base_dir + "all_photo_urls.txt"
	ferr = open(base_dir + "annotations/_errors.txt", "w")

	for i, line in enumerate(open(photos_urls_file)):
		try:
			print i,

			parts = line.strip().split("||")
			p_id = parts[3]
			p_url = parts[4]

			# call api
			resp = api.detection.detect(url=p_url, attribute="gender,age,race,smiling,glass,pose")

			# write to file
			fout = open(base_dir + "annotations/%s.json" % p_id, "w")
			json.dump(resp, fout, indent=4, sort_keys=False)
			fout.close()

			print " ... annotated!"

			#time.sleep(1)

		except Exception as e:
			print e
			print " ... FAILED"
			ferr.write("%d\n" % i)

	ferr.close()

########################################################################################################################

def main():
	#
	# Face++ app config
	#
	key = "***"
	secret = "***"
	api = facepp.API(key, secret)

	# GO!
	base_dir = "../data/NYC/"
	annotate_photos(api, base_dir)

	print "Done!"

########################################################################################################################

if __name__ == "__main__":
    main()

# END